/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */

var invertTree = function(root) {
    if(root === null) return null;
    let temp = root.left;
    root.left = root.right;
    root.right = temp;
    invertTree(root.left);
    invertTree(root.right);
    return root;
};
//创建二叉树
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}
var createTree = function(arr){
    let root = new TreeNode(arr[0]);
    let queue = [root];
    let i = 1;
    while(i < arr.length){
        let node = queue.shift();
        if(arr[i] !== null){
            node.left = new TreeNode(arr[i]);
            queue.push(node.left);
        }
        i++;
    }
  console.log(root);
    return root;
}
console.log(invertTree(createTree([4,2,7,1,3,null,null])));

